package itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import itheima.reggie.common.CustomException;
import itheima.reggie.entity.Category;
import itheima.reggie.entity.Dish;
import itheima.reggie.entity.Setmeal;
import itheima.reggie.mapper.CategoryMapper;
import itheima.reggie.service.CategoryService;
import itheima.reggie.service.DishService;
import itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;
    /**
     * 根据id删除分类，删除之前需要进行判断
     * @param id
     */
    @Override
    public void remove(Long id) {
    //查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count=dishService.count(dishLambdaQueryWrapper);
            if (count>0){
                //抛出一个业务异常类
                throw new CustomException("当前分类已经关联菜品，无法删除！");
            }
    //查询当了分类是否关联了套餐，如果已经关联，抛出业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
            setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
            int count2=setmealService.count(setmealLambdaQueryWrapper);
            if (count2>0){
                //已经关联套餐，抛出一个业务异常
                throw new CustomException("当前分类已经关联套餐，无法删除！");
            }
    //正常删除分类
            super.removeById(id);
    }
}
